
import { getAllContracts } from '@/api/contract'
import utils from '@/components/T/utils'
import _ from 'lodash'


const state = {
  callList: [],
  pullList: [],
  closingDate: [],
  executionPrice: []
}

const mutations = {
  SET_CALL(state, list) {
    state.callList = list
  },
  SET_PULL(state, list) {
    state.pullList = list
  },
  SET_CLOSING_DATE(state, closingDate) {
    state.closingDate = closingDate
  },
  SET_PRICE(state, price) {
    state.executionPrice = price
  }
}

const actions = {
  loadContractData({ commit }, payload) {
    getAllContracts(payload).then(({ result }) => {
      let d = _.chain(result).uniqBy('date').orderBy('date').map('date').value()
      let p = _.chain(result).uniqBy('executionPrice').orderBy('executionPrice').map('executionPrice').value()
      let callList = _.filter(result, ['type', 'C'])
      let pullList = _.filter(result, ['type', 'P'])
      // console.log(callList)

      commit('SET_CLOSING_DATE', d)
      commit('SET_PRICE', p)
      commit('SET_CALL', utils.CCollection(d, p, callList))
      commit('SET_PULL', utils.CCollection(d, p, pullList))
    })
  },
  confirm({ commit }, payload) {
    commit('SET_CALL', payload)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
